/*
 * WordBrowser.h
 *
 *  Created on: May 20, 2009
 *      Author: walter
 */

#ifndef WORDBROWSER_H_
#define WORDBROWSER_H_

#include <iostream>
#include <cassert>
#include <stack>

#include <QTextBrowser>
#include <QProcess>
#include <QShortcut>
#include <QTreeWidgetItem>
#include <QTextCursor>
#include <QDialog>
#include <QFrame>
#include <QAction>
#include <QSound>

#include "Word.h"
#include "DictManager.h"
#include "ui_WordBrowser.h"

using namespace std;

//record browser history
enum BrowseFormType
{
	HT_Test,
	HT_Search,
	HT_Browse
};

//A BrowseForm must be prepared if you want to use WordBrowser::showAll() function
class BrowseForm
{
public:
	int id;
	QString English;
	QString tip;
	QString EnglishFormat;
	BrowseFormType historyType;

	BrowseForm();
	BrowseForm(int i, QString e, QString t, QString ef, BrowseFormType h);
	BrowseForm(const BrowseForm& b);
};

//items used to navigate contents(various articles) in test browser
class NavItem
{
public:
	int anchor;
	QTreeWidgetItem* item;

	NavItem(int an, QTreeWidgetItem* it):
		anchor(an),
		item(it)
	{}
};

class WordBrowser : public QWidget
{
	 Q_OBJECT

public:
	WordBrowser(QWidget * parent = 0 );
	virtual ~WordBrowser();

	void reset();
	void hideArticleNav();
	void showNavList();
	void setNavList();
	//void setThesaurusList(QString key, QString dictName);

	void showAll(BrowseForm& history, bool record = true);
	void showHistory(int pageID);
	void showEnglish(int id = -1, QString format = QString()); //id=-1 indicates current words
	void showSoundMark();
	void showAllExplanations();
	void clearDisplay();
	void insertHtml(QString html);
	//void display(){ui.wordBrowserDisplay->insertHtml(mHtml);}

	QTextBrowser* getTextBrowser(){return ui.wordBrowserDisplay;}

public slots:
	void speak();
	void browserScrollUp();
	void browserScrollDown();
	void itemActivated(QTreeWidgetItem* item, int column);
	void activateNextItem();
	void activatePreviousItem();

	void goToHomePage();
	void goToNextPage();
	void goToPreviousPage();

	void showSearchBar();
	void hideSearchBar();
	void searchWord();

signals:
	//void itemActivatedSig(QTreeWidgetItem* item, int column);
	void searchWord(QString word);

private:
	Ui::WordBrowserClass ui;

	QShortcut* scrollUpShortcut;
	QShortcut* scrollDownShortcut;
	QShortcut* speakButtonClickedShortcut;

	QShortcut* goHomePageShortcut;
	QShortcut* goPreviousPageShortcut;
	QShortcut* goNextPageShortcut;

	QShortcut* showSearchBarShortcut;
	QShortcut* hideSearchBarShortcut;
	QAction* searchWordAction;

	float mNavListWidth;
	QVector<QTreeWidgetItem*> mNavItemListVector;
	map<QTreeWidgetItem*, int> mNavItemListMap;
	QVector<BrowseForm> mBrowseForm;
	QString mLastEnglish;
	int mLastWordID;
	int mCurPageID;
	QTreeWidgetItem* mTopItem;
	QTreeWidgetItem* mArticleRootItem;
	//QTreeWidgetItem* mThesaurusRootItem;
	int mCurItem;
	QString mHtml;

	Word* mCurWord;
};

#endif /* WORDBROWSER_H_ */
